package org.wendu.wdoa.security.role.api;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.security.role.dto.FunDto;
import org.wendu.wdoa.security.role.dto.RoleQueryDto;
import org.wendu.wdoa.security.role.service.RoleService;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleApi {

    @Autowired
    private RoleService roleService;

    /**
     * 分页+模糊查询
     * @param dto
     * @return
     */
    @GetMapping("")
    public Result getRoleList(RoleQueryDto dto) {
        Map<String, Object> rolePage = roleService.getRolePage(dto);
        return Result.OK(rolePage);
    }

    /**
     * 权限查询
     * @return
     */
    @GetMapping("/all-fun")
    public Result funList() {
        List<FunDto> funList = roleService.getFunList();
        return Result.OK(funList);
    }

    /**
     * 新增角色
     * @param roleQueryDto
     * @return
     */
    @PostMapping("/add-role")
    public Result addRole(@RequestBody RoleQueryDto roleQueryDto) {
        roleService.addRole(roleQueryDto);
        return Result.OK();
    }

    /**
     * 单个用户所具有的权限查询
     * @param ro_id
     * @return
     */
    @GetMapping("/fun/{ro_id}")
    public Result roleFunIds(@PathVariable String ro_id) {
        List<Integer> funIds = roleService.getRoleFunIds(ro_id);
        return Result.OK(funIds);
    }

    /**
     * 修改权限
     * @param ro_id
     * @param f_ids
     * @return
     */
    @PutMapping("fun/{ro_id}")
    public Result roleConfirmFuns(@PathVariable String ro_id,@RequestBody Integer[] f_ids) {
        roleService.confirmRoleFuns(ro_id, f_ids);
        return Result.OK();
    }

    @DeleteMapping("delete/{ro_id}")
    public Result deleteDepById(@PathVariable Integer ro_id){
        roleService.deleteRoId(ro_id);
        return Result.OK();
    }

}
